package org.springcloud.wisdom.spring_01_canteen.admin;

import org.springcloud.wisdom.spring_01_canteen.common.bean.Page;
import org.springcloud.wisdom.spring_01_canteen.common.bean.ResponseData;
import org.springcloud.wisdom.spring_01_canteen.login.entity.Mall;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CanMallService {
    @Autowired
    CanMalDao canMalDao;
    //管理员审核商家
    public Page<Mall> getOnrePage(int pageNum, int pageSize, String mrange, String mname){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Mall> list=canMalDao.canmall(start,pageSize,mrange,mname);
        //获取总条目数
        int total=canMalDao.countOnres(mrange,mname);
        //计算总页数
        int pages =(int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Mall> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    //管理员管理商家
    public Page<Mall> getOnrePage1(int pageNum, int pageSize, String mrange, String mname){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Mall> list=canMalDao.canmall1(start,pageSize,mrange,mname);
        //获取总条目数
        int total=canMalDao.countOnres1(mrange,mname);
        //计算总页数
        int pages =(int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Mall> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }
    //根据食堂查商家
    public List<Mall> cmall(String canteen){
        return canMalDao.mall(canteen);
    }

    //修改商家信息
    public boolean updataMall(Mall mall){
        if (mall == null){
            return false;
        }
        return canMalDao.updateMall(mall)>0;
    }

    //修改商家状态
    public boolean approve(Integer mid) {
        return canMalDao.updateStatus(mid, 1) > 0;
    }
    public boolean reject(Integer mid) {
        return canMalDao.updateStatus(mid, 0) > 0;
    }

    //通过
    public boolean updateMall0(Mall mall){
        mall.setIsdel("已同意");
        System.out.println(mall);
        return canMalDao.updateMall(mall)>0;
    }
    //未通过
    public boolean updateMall1(Mall mall){
        mall.setIsdel("已拒绝");
        System.out.println(mall);
        return canMalDao.updateMall(mall)>0;
    }
}
